package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服务器端向客户端响应的内容的类型（MIME）
        resp.setContentType("text/html;charset=utf-8");
        //客户端向服务器端请求的编码格式设置为utf-8
        req.setCharacterEncoding("utf-8");
        //通过response对象获取到PrintWriter打印流
        PrintWriter out = resp.getWriter();
        //调用Service的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.getQueryInfo();
        List<Guestbook> new_list = new ArrayList<>();
        for(int i = 0;i<list.size();i++){
            Guestbook gb = (Guestbook)list.get(i);
            new_list.add(gb);
        }
        //将new_list保存到服务器端
        req.setAttribute("list",list);
        //转发请求
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
